#include "RsGisLibCalKMeans.h"
#include "ExtensionManager.h"
#include "ExtensionSession.h"
#include <QVariant>

RsGisLibCalKMeans::RsGisLibCalKMeans(RsgisPortal* ClientPortal, QWidget *parent)
{
	ui = new Ui::RsGisLibCalKMeans();
	ui->setupUi(this);

	Portal = ClientPortal;

	connect(ui->pushButton_InputFilePath, SIGNAL(clicked()), this, SLOT(on_action_Button_InputFilePath_clicked()));
	connect(ui->pushButton_OutputFilePath, SIGNAL(clicked()), this, SLOT(on_action_Button_OutputFilePath_clicked()));
	connect(ui->pushButton_RunCluster, SIGNAL(clicked()), this, SLOT(on_action_Button_RunClustering_clicked()));

	ui->textBrowser_ParameterDescription->setText(
		"n_Clusters - is the number of clusters to use\nMax_n_Iters - is the maximum number of itterations\nSub_Sample - is an int specifying what fraction of the total pixels should be considered(e.g., 100 = 1 / 100 pixels)\nIgnore_Zeros - is a bool specifying if zeros in the image should be treated as no data\nDegree_Change - is a float providing the minimum change between iterations before terminating\nCluster_Method - the method for initialising the clusters and is one of INITCLUSTER_* values"
	);

	ui->textBrowser_ParameterDescription->setEnabled(false);

	QStringList ClusterMethod;
	ClusterMethod.append("Random");
	ClusterMethod.append("Diagonal_full");
	ClusterMethod.append("Diagonal_stddev");
	ClusterMethod.append("Full_attach");
	ClusterMethod.append("Stddev_attach");
	ClusterMethod.append("Lpp");
	ui->comboBox_InitClusterMethod->addItems(ClusterMethod);

	QStringList IgnoreZeroList;
	IgnoreZeroList.append("True");
	IgnoreZeroList.append("False");
	ui->comboBox_IgnoreZero->addItems(IgnoreZeroList);

	ui->lineEdit_InputFilePath->setText("D:\\RS-RPC\\TEST\\IN\\RsGisLib\\Test.tif");
	ui->lineEdit_OutputFilePath->setText("D:\\RS-RPC\\TEST\\IN\\RsGisLib\\TestKMeans");

	ui->lineEdit_nClusters->setText("10");
	ui->lineEdit_MaxnIters->setText("200");
	ui->lineEdit_SubSample->setText("1");
	ui->comboBox_IgnoreZero->setCurrentIndex(0);
	ui->lineEdit_DegreeChange->setText("0.0025");
	ui->comboBox_InitClusterMethod->setCurrentIndex(3);

	ui->lineEdit_Priority->setText("16");
}

void RsGisLibCalKMeans::on_action_Button_InputFilePath_clicked()
{
	QString str = QFileDialog::getOpenFileName(this, "File Explorer", QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation), "Tif file(*.tif *.TIF);;All file(*.*)");
	ui->lineEdit_InputFilePath->setText(str.toUtf8());
}

void RsGisLibCalKMeans::on_action_Button_OutputFilePath_clicked()
{
	QString str = QFileDialog::getSaveFileName(this, "File Explorer", QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation), "Tif file(*.tif *.TIF);;All file(*.*)");
	ui->lineEdit_OutputFilePath->setText(str.toUtf8());
}

void RsGisLibCalKMeans::on_action_Button_RunClustering_clicked()
{
	{
		if (ui->lineEdit_InputFilePath->text().isEmpty())
		{
			QMessageBox::warning(NULL, "Error", "Input File Path is Null", QMessageBox::Yes, QMessageBox::Yes);
			return;
		}

		if (ui->lineEdit_OutputFilePath->text().isEmpty())
		{
			QMessageBox::warning(NULL, "Error", "Output File Path is Null", QMessageBox::Yes, QMessageBox::Yes);
			return;
		}

		if (ui->lineEdit_nClusters->text().isEmpty())
		{
			QMessageBox::warning(NULL, "Error", "n_Clusters is Null", QMessageBox::Yes, QMessageBox::Yes);
			return;
		}

		if (ui->lineEdit_SubSample->text().isEmpty())
		{
			QMessageBox::warning(NULL, "Error", "Sub_Sample is Null", QMessageBox::Yes, QMessageBox::Yes);
			return;
		}

		if (ui->lineEdit_DegreeChange->text().isEmpty())
		{
			QMessageBox::warning(NULL, "Error", "Degree_Change is Null", QMessageBox::Yes, QMessageBox::Yes);
			return;
		}

		if (ui->lineEdit_Priority->text().isEmpty())
		{
			QMessageBox::warning(NULL, "Error", "Priority is Null", QMessageBox::Yes, QMessageBox::Yes);
			return;
		}
	}

	QList<QVariant> SysParaList;
	SysParaList << QVariant(QString("RsGisLib"));
	SysParaList << QVariant(QString("imageCalculation"));
	SysParaList << QVariant(int(ui->lineEdit_Priority->text().toInt()));

	QList<QVariant> AlgParaList;
	AlgParaList << QVariant(QString("KMeans"));
	AlgParaList << QVariant(QString(ui->lineEdit_InputFilePath->text()));
	AlgParaList << QVariant(QString(ui->lineEdit_OutputFilePath->text()));
	AlgParaList << QVariant(int(ui->lineEdit_nClusters->text().toInt()));
	AlgParaList << QVariant(int(ui->lineEdit_MaxnIters->text().toInt()));
	AlgParaList << QVariant(int(ui->lineEdit_SubSample->text().toInt()));
	AlgParaList << QVariant(int(ui->comboBox_IgnoreZero->currentIndex()));
	AlgParaList << QVariant(double(ui->lineEdit_DegreeChange->text().toDouble()));
	AlgParaList << QVariant(int(ui->comboBox_InitClusterMethod->currentIndex()));

	QVariantList ArgList;
	ArgList << (QVariant)SysParaList;
	ArgList << (QVariant)AlgParaList;

	Portal->goAhead(ArgList);
}